package w83b.w83bOperacionesPaso.w83bMantenimientos.w83bSindicatos;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import w83b.w83bBeans.W83bSindicatosBean;
import w83b.w83bUtilidades.W83bUtilidadesComunes;
import w83b.w83bUtilidadesComunes.W83bClsConstantes;

import es.ejie.frmk.presentacion.operacion.paso.Q70ActionPasoOP;
import es.ejie.frmk.presentacion.sesiondatos.Q70ContextoEJIE;

// TODO: Auto-generated Javadoc
/**
 * The Class W83bTablaAgrupacionesOP.java.
 *
 * @author Deusto Sistemas
 */
public class W83bTablaSindicatosOPAction extends Q70ActionPasoOP{

	
	/* (non-Javadoc)
	 * @see es.ejie.frmk.presentacion.operacion.paso.Q70ActionPasoOP#processOP(es.ejie.frmk.presentacion.sesiondatos.Q70ContextoEJIE, org.apache.struts.action.ActionErrors, org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	protected void processOP(Q70ContextoEJIE contexto, ActionErrors errores, ActionMapping mapping,
			ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		//Guardamos la accion en el contexto
			contexto.set("action",request.getParameter("action"));
			W83bSindicatosBean sindicatosBean = (W83bSindicatosBean)contexto.get("w83bSindicatosBean");
		if((StringUtils.isEmpty((String)contexto.get("modoPantalla"))
				||W83bClsConstantes.MODO_BUSQUEDA.equals(contexto.get("modoPantalla")))
				&&(!"sord".equals(request.getParameter("action")))){
			if("true".equals(request.getParameter(W83bClsConstantes.RIA_SEARCH))){
				sindicatosBean = new W83bSindicatosBean();
				sindicatosBean.setCodSindicato(request.getParameter("w83bSindicatosBean.codSindicato"));
				sindicatosBean.setDescSindicatoCast(request.getParameter("w83bSindicatosBean.descSindicatoCast"));
				sindicatosBean.setDescSindicatoEusk(request.getParameter("w83bSindicatosBean.descSindicatoEusk"));
				sindicatosBean.setSiglas(request.getParameter("w83bSindicatosBean.siglas"));
				sindicatosBean.setCodVerificacion(request.getParameter("w83bSindicatosBean.codVerificacion"));
				sindicatosBean.setCodMadrid(request.getParameter("w83bSindicatosBean.codMadrid"));
				//sindicatosBean.setStes(request.getParameter("w83bSindicatosBean.stes"));
				sindicatosBean.setBaja(request.getParameter("w83bSindicatosBean.baja"));
				sindicatosBean = (W83bSindicatosBean)W83bUtilidadesComunes.inicializarPaginador(request, sindicatosBean);
				sindicatosBean.setPage("1");
				contexto.set("w83bSindicatosBean",sindicatosBean);
			}
			else{
				sindicatosBean = (W83bSindicatosBean)W83bUtilidadesComunes.inicializarPaginador(request, sindicatosBean);
				//sindicatosBean.setStes("N");
				contexto.set("w83bSindicatosBean",sindicatosBean);	
			}
		}else if("sord".equals(request.getParameter("action"))){
			sindicatosBean = (W83bSindicatosBean)W83bUtilidadesComunes.inicializarPaginador(request, sindicatosBean);			
			contexto.set("w83bSindicatosBean",sindicatosBean);
		}else if(StringUtils.isEmpty(((W83bSindicatosBean)contexto.get("w83bSindicatosBean")).getSort())){
			sindicatosBean = (W83bSindicatosBean)W83bUtilidadesComunes.inicializarPaginador(request, sindicatosBean);			
			contexto.set("w83bSindicatosBean",sindicatosBean);
		}
		
	}

	/* (non-Javadoc)
	 * @see es.ejie.frmk.presentacion.operacion.paso.Q70ActionPasoOP#processForward(es.ejie.frmk.presentacion.sesiondatos.Q70ContextoEJIE, org.apache.struts.action.ActionErrors, org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	protected ActionForward processForward(Q70ContextoEJIE contexto, ActionErrors errors, ActionMapping mapping, 
			ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		String action = (String)contexto.get("action");

		W83bSindicatosBean sindicatosBean = (W83bSindicatosBean)contexto.get("w83bSindicatosBean");
		
		if(null==(ArrayList)contexto.get("w83bListaSindicatos")){	
			contexto.set("w83bListaSindicatos", new ArrayList());
		}
		if(null==sindicatosBean.getCodSindicato()
				&&null==sindicatosBean.getDescSindicatoCast()
				&&null==sindicatosBean.getDescSindicatoEusk()
				&&null==sindicatosBean.getSiglas()
				&&null==sindicatosBean.getCodVerificacion()
				&&null==sindicatosBean.getCodMadrid()
				//&&null==sindicatosBean.getStes()
				){
			return mapping.findForward("find");
		}else if ("find".equals(action)
				||"sord".equals(action)){
			//Si vengo de una pantalla anterior tengo que mirar si he realizado una búsqueda anterior
			if(StringUtils.isEmpty((String)contexto.get("modoPantalla"))
					||W83bClsConstantes.MODO_BUSQUEDA.equals(contexto.get("modoPantalla"))){
				return mapping.findForward("find");
			}else{
				contexto.set("modoPantalla",W83bClsConstantes.MODO_BUSQUEDA);
				contexto.set("migas","inicio.mantenimiento.sindicatos");
				if("true".equals((String)contexto.get("busqRealizada"))){
					return mapping.findForward("find");					
				}
			}
		}

		
		return mapping.findForward("ok");
	}

}
